const list = document.getElementById('sortable');

var dragged = null;

list.addEventListener('dragstart',function(event){
    dragged=event.target;
})

list.addEventListener('dragover',function(event){
    event.preventDefault();
})

list.addEventListener('drop',function(event){
    var dropped=event.target;
    var dragIndex=getIndext(dragged);
    var dropIndex=getIndext(dropped);
    if(dragIndex>dropIndex){
        list.insertBefore(dragged,dropped);
    }else{
        list.insertBefore(dragged,dropped.nextSibling);
    }
})

function getIndext(node){
    let index=0;
    while(node=node.previousElementSibling){
        index++;
    }
    return index;
}